package com.stenio.fancat.controller.wx;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.shop.FcEtOrder;
import com.stenio.fancat.domain.shop.FcGift;
import com.stenio.fancat.domain.user.UserRelationship;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.mapper.user.UserRelationshipMapper;
import com.stenio.fancat.service.shop.GiftService;
import com.stenio.fancat.service.wx.FcEtOrderService;
import com.stenio.fancat.util.EmptyUtil;
import com.stenio.fancat.vo.EtOrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuan
 * @create 2019-02-26 16:44
 */

@RestController
@RequestMapping("etOrder")
public class FcEtOrderController {

    @Autowired
    FcEtOrderService fcEtOrderService;

    @Autowired
    GiftService giftService;

    @Autowired
    UserRelationshipMapper userRelationshipMapper;

    @PostMapping("extractAmount/{id}/{babyId}")
    public Result extractAmount(@PathVariable String id,@PathVariable String babyId) {
        Result result = new Result();
        Integer total = 0;
        try {
            total = fcEtOrderService.extractAmount(id,babyId);
        } catch (FanCatException e) {
            result.setFlag(Result.FLAG.failure);
            result.setMsg(e.getMessage());
            return result;
        }
        result.setData(total);
        return result;
    }

    @PostMapping("findTotalAmount/{id}/{babyId}")
    public Result findTotalAmount(@PathVariable String id,@PathVariable String babyId) {
        Result result = new Result();
        List<FcEtOrder> fcEtOrders = fcEtOrderService.selectList(new EntityWrapper<FcEtOrder>().eq(FcEtOrder.USER_ID, id)
                .eq(FcEtOrder.STATUS,1)
                .eq(FcEtOrder.BABY_ID,babyId));
        if(EmptyUtil.isEmpty(fcEtOrders)) {
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        Integer total = 0;
        for (FcEtOrder fcEtOrder : fcEtOrders) {
            total += fcEtOrder.getReflectAmount();
        }
        result.setData(total);
        return result;
    }

    @PostMapping("findEtOrder/{id}/{babyId}")
    public Result findEtOrder(@PathVariable String id,@PathVariable String babyId) {
        Result result = new Result();
        List<FcEtOrder> fcEtOrders = fcEtOrderService.selectList(new EntityWrapper<FcEtOrder>().eq(FcEtOrder.USER_ID, id)
        .eq(FcEtOrder.BABY_ID,babyId)
        .eq(FcEtOrder.STATUS,1));
        if(EmptyUtil.isEmpty(fcEtOrders)) {
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        Map<String,List> etOrders = new HashMap<>();
        SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日");
        for(FcEtOrder etOrder : fcEtOrders) {
            String format = s.format(etOrder.getCreateTime());
            if(etOrders.containsKey(format)) {
                etOrders.get(format).add(redOrderToVo(etOrder));
            }else {
                List newList = new ArrayList<>();
                newList.add(redOrderToVo(etOrder));
                etOrders.put(format,newList);
            }
        }
        List orderList = new ArrayList();
        for(Map.Entry entry : etOrders.entrySet()) {
            Map newMap = new HashMap();
            newMap.put("dateTime",entry.getKey());
            newMap.put("etOrder",entry.getValue());
            orderList.add(newMap);
        }
        result.setData(orderList);
        return result;
    }

    private EtOrderVo redOrderToVo(FcEtOrder fcEtOrder) {
        EtOrderVo etOrderVo = new EtOrderVo();
        BeanUtils.copyProperties(fcEtOrder,etOrderVo);
        UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(fcEtOrder.getEtUserId());
        FcGift fcGift = giftService.selectById(fcEtOrder.getGiftId());
        if(userRelationship != null) {
            etOrderVo.setUserName(userRelationship.getName());//TODO
            etOrderVo.setUserImage(userRelationship.getIcon());
            etOrderVo.setGiftName(fcGift.getName());
            etOrderVo.setGiftImage(fcGift.getImage());
        }
        return etOrderVo;
    }

}
